﻿$(function() {

    // Setup the tabs
    $('#tab_content > ul').tabs();

    // Show a message
    $.showMessage = function(message, messageId, callback) {
        if (messageId == undefined) {
            messageId = '#message';
        }
        $(messageId).html(message);
        $(messageId).show().animate({opacity: 1.0}, 1000).slideUp('normal', callback);
    }
    
    // Navigate to a specific page
    $.navigateToPageById = function(id) {
        var url = "../../index.lasso?p=" + id + "&l=" + $('title').attr('lang');
        window.location.href = url;
    }
    
    // Close a modal dialog box
    $.closeModal = function(type, data) {
        $('p#' + type + '_desc').hide();
        $('form#' + type + '_title').hide();
        $.showMessage(data, '#' + type + '_title_msg', function() { $.modal.close(); });
    }
    
    // Get the values from the list of form elements
    $.getFormValues = function(elements) {
        var result = {};
        for (i in elements) {
            if ($('#' + elements[i]).attr('type') == 'radio') {
                result[elements[i]] = $('input[@name="' + elements[i] + '"]:checked').val();
            } else {
            	// kan hieronder waarschijnlijk nog geoptimaliseerd worden door de radio & checkbox samen te steken hierboven
            	 if ($('#' + elements[i]).attr('type') == 'checkbox') {
               		 result[elements[i]] = $('input[@name="' + elements[i] + '"]:checked').val();
           		 } else {
              		  result[elements[i]] = $('#' + elements[i]).val();
           		 }
            }
        }
        return result;
    }

    // save page
    $('#savepage').click(function() {
        // TinyMCE bug workaround for iframe
        tinyMCE.triggerSave(true,true);
        $.post(
            "savepage.lasso",
            $.getFormValues(['pageform_title', 'pageform_content', 'pageform_lang', 'pageform_page']),
            function(data){
                $.showMessage(data);
            }
        );
    });
    
    // save meta data
    $('#savemeta').click(function() {
        $.post(
            "savemeta.lasso",
            $.getFormValues(['meta_title', 'meta_description', 'meta_keywords', 'pageform_lang', 'pageform_page']),
            function(data){
                $.showMessage(data);
            }
        );
    });
    
    
    // save behaviour 
    $('#savebehaviour').click(function() {
        $.post(
            "savebehaviour.lasso",
            $.getFormValues(['menu_active', 'pageform_lang', 'pageform_page']),
            function(data){
                $.showMessage(data);
            }
        );
    });
    
    

    // save url 
    $('#saveurl').click(function() {
        $.post(
            "saveurl.lasso",
            $.getFormValues(['pageform_title', 'pageform_url', 'pageform_lang', 'pageform_page', 'pageform_target']),
            function(data){
                $("a[menuId='" + $('#pageform_menu').val() + "']").html($('#pageform_title').val());
                $.showMessage(data);
            }
        );
    });

    // add page
    $('#tree_actions_submit').click(function() {
        $.post(
            "addpage.lasso",
            $.getFormValues(['tree_currentpageid', 'tree_langid', 'tree_actions_submit', 'tree_pagetype']),
            function(data){
                $.navigateToPageById(data);
            }
        );
    });
            
    // rename menu from right click
    $('#rename_submit').click(function() {
        $('#rename_title').submit();
    });
    
    $('#rename_title').submit(function() {
        $.post(
            "rename_menu.lasso",
            $.getFormValues(['menuform_title', 'menuform_lang', 'menuform_menu']),
            function(data){
                $("a[menuId='" + $('#menuform_menu').val() + "']").html($('#menuform_title').val());
                $.closeModal('rename', data);
            });
        return false;
    });

    // delete menu from right click
    $('#delete_submit').click(function() {
        $('#delete_title').submit();
    });

    $('#delete_title').submit(function() {
        $.post(
            "delete_menu.lasso",
            $.getFormValues(['menuform_title', 'menuform_lang', 'menuform_menu']),
            function(data){
                $("a[menuId='" + $('#menuform_menu').val() + "']").remove();
                $.closeModal('delete', data);
                $.navigateToPageById(1);
            });
        return false;
    });
    
    //Contextual menus for tree
    $('ul').contextMenu('contextMenu', {
    
        onContextMenu: function(e) {
            return $(e.target).attr('id') != 'dontShow';
        },

        onShowMenu: function(e, menu) {
            var mijntarget = $(e.target).attr('menuId');
            var mijntitel = $(e.target).html();
            var mijntaal = $(e.target).attr('langId');
 
            $('#menuform_title').val(mijntitel);
            $('#menuform_lang').val(mijntaal);
            $('#menuform_menu').val(mijntarget);
            $('b#pageTitle').html(mijntitel);

            return menu;
        },
 
        menuStyle: {
            border: '1px solid #000000'
        },

        itemStyle: {
            fontFamily : 'verdana',
            backgroundColor : '#FFFFFF',
            color: 'black',
            border: 'none',
            padding: '1px'
        },
 
        itemHoverStyle: {
            color: '#000000',
            backgroundColor: '#EEEEEE',
            border: 'none'
        }
     
    });  
    
    // Modal Lightbox for contextual menu
    $('a#ModalRename').click(function (e){
        e.preventDefault();
        $('#RenameModalContent').modal();
        return false;
    });
        
        
    $('a#ModalDelete').click(function (e) {
        e.preventDefault();
        $('#DeleteModalContent').modal();
        return false;
    });
                
});